

-- Namespace
	local _,mod = ...


-- Auto-sell greys
	mod:event_register('MERCHANT_SHOW',function()
		local link
		for bag = 0,4 do
			for slot = 0,GetContainerNumSlots(bag) do
				link = GetContainerItemLink(bag,slot)
				if link and select(3,GetItemInfo(link)) == 0 then
					ShowMerchantSellCursor(1)
					UseContainerItem(bag,slot)
				end
			end
		end
	end)


-- Auto-repair
	mod:event_register('MERCHANT_SHOW',function()
		if not CanMerchantRepair() then return end
		
		local cost,can = GetRepairAllCost()
		if not can then return end
		
		local funds
		
		if CanGuildBankRepair() then
			funds = GetGuildBankMoney() or 0
			if funds > cost*2 then
				RepairAllItems(1)
				return
			end
		end
		
		funds = GetMoney() or 0
		if funds > cost*2 then
			RepairAllItems()
			return
		end
		
		print('|cffffff7fnfExtras|r: Tried to auto-repair, but you could not afford it.')
	end)


-- Slash commands: common
	local editbox = CreateFrame('EditBox','nfExtrasEditBox')
		editbox:SetAutoFocus(false) editbox:ClearFocus()


-- Slash commands: /in
	local active = {}
	
	SLASH_NFIN1 = '/in'
	SlashCmdList['NFIN'] = function(msg)
		local delay,name,cmd,args = msg:match('^([%d.]+)%s+%[(%S+)%]%s+(/%S+)(.*)$')
			delay = tonumber(delay)
		
		if not delay then
			delay,cmd,args = msg:match('^([%d.]+)%s+(/%S+)(.*)$')
			delay = tonumber(delay)
		end
		
		if not delay then
			print('|cff7fffff/in|r: Incorrect usage. Format: |cffffff7f/in 2.5 /say Hi|r.')
			return
		end
		
		if IsSecureCmd(cmd) then
			print('|cff7fffff/in|r: Cannot automate secure command |cffffff7f'..cmd..'|r.')
			return
		end
		
		local func
		cmd = cmd..args
		
		if name then
			if active[name] then
				return
			else
				active[name] = true
				func = function()
					editbox:SetText(cmd)
					ChatEdit_SendText(editbox)
					active[name] = nil
				end
			end
		else
			func = function()
				editbox:SetText(cmd)
				ChatEdit_SendText(editbox)
			end
		end
		
		mod:timer_register(delay,false,func)
	end


-- Slash commands: /eval
	SLASH_NFEVAL1 = '/eval'
	SLASH_NFEVAL2 = '/ev'
	SlashCmdList['NFEVAL'] = function(input)
		local result = SecureCmdOptionParse(input)
		if not result then return end
		
		local cmd,args = result:match('^(/%S+)(.*)$')
		if cmd then  -- If they passed a slash-command, run it.
			if IsSecureCmd(cmd) then
				print('|cff7fffff/eval|r: Cannot automate secure command |cffffff7f'..cmd..'|r.')
				return
			end
			
			editbox:SetText(cmd..args)
			ChatEdit_SendText(editbox)
			
			return
		end
		
		-- If they didn't pass a slash-command, print it.
		print('|cff7fffff/eval|r: '..result)
	end


-- Slash commands: /rl
	SLASH_NFRL1 = '/rl'
	SlashCmdList['NFRL'] = ReloadUI


-- Slash commands: /pl
	SLASH_NFLP1 = '/lp'
	SlashCmdList['NFLP'] = LeaveParty


-- Slash commands: /ve
	SLASH_NFVE1 = '/ve'
	SlashCmdList['NFVE'] = VehicleExit
